home *** CD-ROM | disk | FTP | other *** search
/ Personal Computer World 2007 September / PCWSEP07.iso / Software / Resources / Developers / XAMPP 1.5.4 / Windows installer / xampp-win32-1.5.4-installer.exe / xampp / php / pear / Pager / Jumping.php < prev    next >
Encoding:
PHP Script  |  2005-10-16  |  9.1 KB  |  265 lines

  1. <?php
  2. /* vim: set expandtab tabstop=4 shiftwidth=4 softtabstop=4: */
  3.  
  4. /**
  5.  * Contains the Pager_Jumping class
  6.  *
  7.  * PHP versions 4 and 5
  8.  *
  9.  * PHP versions 4 and 5
  10.  *
  11.  * LICENSE: Redistribution and use in source and binary forms, with or without
  12.  * modification, are permitted provided that the following conditions are met:
  13.  * 1. Redistributions of source code must retain the above copyright
  14.  *    notice, this list of conditions and the following disclaimer.
  15.  * 2. Redistributions in binary form must reproduce the above copyright
  16.  *    notice, this list of conditions and the following disclaimer in the
  17.  *    documentation and/or other materials provided with the distribution.
  18.  * 3. The name of the author may not be used to endorse or promote products
  19.  *    derived from this software without specific prior written permission.
  20.  *
  21.  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR "AS IS" AND ANY EXPRESS OR IMPLIED
  22.  * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
  23.  * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
  24.  * IN NO EVENT SHALL THE FREEBSD PROJECT OR CONTRIBUTORS BE LIABLE FOR ANY
  25.  * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
  26.  * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
  27.  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
  28.  * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  29.  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
  30.  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  31.  *
  32.  * @category   HTML
  33.  * @package    Pager
  34.  * @author     Lorenzo Alberton <l dot alberton at quipo dot it>
  35.  * @author     Richard Heyes <richard@phpguru.org>,
  36.  * @copyright  2003-2005 Lorenzo Alberton, Richard Heyes
  37.  * @license    http://www.debian.org/misc/bsd.license  BSD License (3 Clause)
  38.  * @version    CVS: $Id: Jumping.php,v 1.9 2005/07/22 16:28:11 quipo Exp $
  39.  * @link       http://pear.php.net/package/Pager
  40.  */
  41.  
  42. /**
  43.  * require PEAR::Pager_Common base class
  44.  */
  45. require_once 'Pager/Common.php';
  46.  
  47. /**
  48.  * Pager_Jumping - Generic data paging class  ("jumping window" style)
  49.  * Handles paging a set of data. For usage see the example.php provided.
  50.  *
  51.  * @category   HTML
  52.  * @package    Pager
  53.  * @author     Lorenzo Alberton <l dot alberton at quipo dot it>
  54.  * @author     Richard Heyes <richard@phpguru.org>,
  55.  * @copyright  2003-2005 Lorenzo Alberton, Richard Heyes
  56.  * @license    http://www.php.net/license/3_0.txt  PHP License 3.0
  57.  * @link       http://pear.php.net/package/Pager
  58.  */
  59. class Pager_Jumping extends Pager_Common
  60. {
  61.     // {{{ Pager_Jumping()
  62.  
  63.     /**
  64.      * Constructor
  65.      *
  66.      * @param mixed $options    An associative array of option names
  67.      *                          and their values
  68.      * @access public
  69.      */
  70.     function Pager_Jumping($options = array())
  71.     {
  72.         $err = $this->_setOptions($options);
  73.         if ($err !== PAGER_OK) {
  74.             return $this->raiseError($this->errorMessage($err), $err);
  75.         }
  76.         $this->_generatePageData();
  77.         $this->_setFirstLastText();
  78.  
  79.         $this->links .= $this->_getBackLink();
  80.         $this->links .= $this->_getPageLinks();
  81.         $this->links .= $this->_getNextLink();
  82.  
  83.         $this->linkTags .= $this->_getFirstLinkTag();
  84.         $this->linkTags .= $this->_getPrevLinkTag();
  85.         $this->linkTags .= $this->_getNextLinkTag();
  86.         $this->linkTags .= $this->_getLastLinkTag();
  87.     }
  88.  
  89.     // }}}
  90.     // {{{ getPageIdByOffset()
  91.  
  92.     /**
  93.      * Returns pageID for given offset
  94.      *
  95.      * @param $index Offset to get pageID for
  96.      * @return int PageID for given offset
  97.      */
  98.     function getPageIdByOffset($index)
  99.     {
  100.         if (!isset($this->_pageData)) {
  101.             $this->_generatePageData();
  102.         }
  103.  
  104.         if (($index % $this->_perPage) > 0) {
  105.             $pageID = ceil((float)$index / (float)$this->_perPage);
  106.         } else {
  107.             $pageID = $index / $this->_perPage;
  108.         }
  109.         return $pageID;
  110.     }
  111.  
  112.     // }}}
  113.     // {{{ getPageRangeByPageId()
  114.  
  115.     /**
  116.      * Given a PageId, it returns the limits of the range of pages displayed.
  117.      * While getOffsetByPageId() returns the offset of the data within the
  118.      * current page, this method returns the offsets of the page numbers interval.
  119.      * E.g., if you have pageId=3 and delta=10, it will return (1, 10).
  120.      * PageID of 8 would give you (1, 10) as well, because 1 <= 8 <= 10.
  121.      * PageID of 11 would give you (11, 20).
  122.      * If the method is called without parameter, pageID is set to currentPage#.
  123.      *
  124.      * @param integer PageID to get offsets for
  125.      * @return array  First and last offsets
  126.      * @access public
  127.      */
  128.     function getPageRangeByPageId($pageid = null)
  129.     {
  130.         $pageid = isset($pageid) ? (int)$pageid : $this->_currentPage;
  131.         if (isset($this->_pageData[$pageid]) || is_null($this->_itemData)) {
  132.             // I'm sure I'm missing something here, but this formula works
  133.             // so I'm using it until I find something simpler.
  134.             $start = ((($pageid + (($this->_delta - ($pageid % $this->_delta))) % $this->_delta) / $this->_delta) - 1) * $this->_delta +1;
  135.             return array(
  136.                 max($start, 1),
  137.                 min($start+$this->_delta-1, $this->_totalPages)
  138.             );
  139.         } else {
  140.             return array(0, 0);
  141.         }
  142.     }
  143.  
  144.     // }}}
  145.     // {{{ getLinks()
  146.  
  147.     /**
  148.      * Returns back/next/first/last and page links,
  149.      * both as ordered and associative array.
  150.      *
  151.      * NB: in original PEAR::Pager this method accepted two parameters,
  152.      * $back_html and $next_html. Now the only parameter accepted is
  153.      * an integer ($pageID), since the html text for prev/next links can
  154.      * be set in the constructor. If a second parameter is provided, then
  155.      * the method act as it previously did. This hack's only purpose is to
  156.      * mantain backward compatibility.
  157.      *
  158.      * @param integer $pageID Optional pageID. If specified, links
  159.      *                for that page are provided instead of current one.
  160.      *                [ADDED IN NEW PAGER VERSION]
  161.      * @param  string $next_html HTML to put inside the next link
  162.      *                [deprecated: use the constructor instead]
  163.      * @return array Back/pages/next links
  164.      */
  165.     function getLinks($pageID=null, $next_html='')
  166.     {
  167.         //BC hack
  168.         if (!empty($next_html)) {
  169.             $back_html = $pageID;
  170.             $pageID    = null;
  171.         } else {
  172.             $back_html = '';
  173.         }
  174.  
  175.         if (!is_null($pageID)) {
  176.             $_sav = $this->_currentPage;
  177.             $this->_currentPage = $pageID;
  178.  
  179.             $this->links = '';
  180.             if ($this->_totalPages > $this->_delta) {
  181.                 $this->links .= $this->_printFirstPage();
  182.             }
  183.             $this->links .= $this->_getBackLink('', $back_html);
  184.             $this->links .= $this->_getPageLinks();
  185.             $this->links .= $this->_getNextLink('', $next_html);
  186.             if ($this->_totalPages > $this->_delta) {
  187.                 $this->links .= $this->_printLastPage();
  188.             }
  189.         }
  190.  
  191.         $back  = str_replace(' ', '', $this->_getBackLink());
  192.         $next  = str_replace(' ', '', $this->_getNextLink());
  193.         $pages = $this->_getPageLinks();
  194.         $first = $this->_printFirstPage();
  195.         $last  = $this->_printLastPage();
  196.         $all   = $this->links;
  197.         $linkTags = $this->linkTags;
  198.  
  199.         if (!is_null($pageID)) {
  200.             $this->_currentPage = $_sav;
  201.         }
  202.  
  203.         return array(
  204.             $back,
  205.             $pages,
  206.             trim($next),
  207.             $first,
  208.             $last,
  209.             $all,
  210.             $linkTags,
  211.             'back'  => $back,
  212.             'pages' => $pages,
  213.             'next'  => $next,
  214.             'first' => $first,
  215.             'last'  => $last,
  216.             'all'   => $all,
  217.             'linktags' => $linkTags
  218.         );
  219.     }
  220.  
  221.     // }}}
  222.     // {{{ _getPageLinks()
  223.  
  224.     /**
  225.      * Returns pages link
  226.      *
  227.      * @param $url  URL to use in the link
  228.      *              [deprecated: use the constructor instead]
  229.      * @return string Links
  230.      * @access private
  231.      */
  232.     function _getPageLinks($url = '')
  233.     {
  234.         //legacy setting... the preferred way to set an option now
  235.         //is adding it to the constuctor
  236.         if (!empty($url)) {
  237.             $this->_path = $url;
  238.         }
  239.  
  240.         //If there's only one page, don't display links
  241.         if ($this->_clearIfVoid && ($this->_totalPages < 2)) {
  242.             return '';
  243.         }
  244.  
  245.         $links = '';
  246.         $limits = $this->getPageRangeByPageId($this->_currentPage);
  247.  
  248.         for ($i=$limits[0]; $i<=min($limits[1], $this->_totalPages); $i++) {
  249.             if ($i != $this->_currentPage) {
  250.                 $this->range[$i] = false;
  251.                 $this->_linkData[$this->_urlVar] = $i;
  252.                 $links .= $this->_renderLink($this->_altPage.' '.$i, $i);
  253.             } else {
  254.                 $this->range[$i] = true;
  255.                 $links .= $this->_curPageSpanPre . $i . $this->_curPageSpanPost;
  256.             }
  257.             $links .= $this->_spacesBefore
  258.                    . (($i != $this->_totalPages) ? $this->_separator.$this->_spacesAfter : '');
  259.         }
  260.         return $links;
  261.     }
  262.  
  263.     // }}}
  264. }
  265. ?>